package org.ru.mse10.cvis.web.bean.action.search;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import org.ru.mse10.cvis.dto.cv.CustomFieldDTO;
import org.ru.mse10.cvis.entity.cv.custom.CustomField;
import org.ru.mse10.cvis.service.CustomFieldsService;
import org.ru.mse10.cvis.service.search.CustomFieldSearcher;
import org.ru.mse10.cvis.service.search.EntitySearcher;
import org.ru.mse10.cvis.util.BooleanSelection;
import org.ru.mse10.cvis.web.bean.action.cv.CustomFieldsAction;
import org.ru.mse10.cvis.web.bean.action.search.datamodel.JPADataModel;

/**
 * The Class CustomFieldsSearchAction.
 */
@ViewScoped
@ManagedBean(name = "customFieldsSearchAction")
public class CustomFieldsSearchAction extends
		BasesSearchAction<CustomFieldsService, CustomField, CustomFieldDTO> {

	/** The service. */
	@EJB
	private CustomFieldsService service;

	/** The custom fields action. */
	@ManagedProperty("#{customFieldsAction}")
	private CustomFieldsAction customFieldsAction;

	/**
	 * The Class CustomFieldsSearchDataModel.
	 */
	private class CustomFieldsSearchDataModel extends
			JPADataModel<CustomFieldsService, CustomField, CustomFieldDTO> {

		/** The dto. */
		private CustomFieldDTO dto;

		/** The searcher. */
		private CustomFieldSearcher searcher;

		/**
		 * Instantiates a new custom fields search data model.
		 * 
		 * @param dto
		 *            the dto
		 */
		public CustomFieldsSearchDataModel(CustomFieldDTO dto) {
			this.dto = dto;
			this.searcher = new CustomFieldSearcher();
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.ru.mse10.cvis.web.bean.action.search.datamodel.JPADataModel#
		 * getSearcher()
		 */
		@Override
		public EntitySearcher<CustomField, CustomFieldDTO> getSearcher() {
			return searcher;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.ru.mse10.cvis.web.bean.action.search.datamodel.JPADataModel#
		 * getService()
		 */
		@Override
		public CustomFieldsService getService() {
			return CustomFieldsSearchAction.this.getService();
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.ru.mse10.cvis.web.bean.action.search.datamodel.JPADataModel#getDto
		 * ()
		 */
		@Override
		public CustomFieldDTO getDto() {
			return dto;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.ru.mse10.cvis.web.bean.action.search.datamodel.JPADataModel#
		 * getEntityClass()
		 */
		@Override
		public Class<CustomField> getEntityClass() {
			return CustomField.class;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ru.mse10.cvis.web.bean.action.search.BasesSearchAction#
	 * previewSearchResult()
	 */
	@Override
	public String previewSearchResult() {
		return customFieldsAction.loadForPreview(service.find(
				CustomField.class, getSelectedId()));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ru.mse10.cvis.web.bean.action.search.BasesSearchAction#getService()
	 */
	@Override
	public CustomFieldsService getService() {
		return service;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ru.mse10.cvis.web.bean.action.search.BasesSearchAction#createDataModel
	 * (org.ru.mse10.cvis.dto.BaseDTO)
	 */
	@Override
	public JPADataModel<CustomFieldsService, CustomField, CustomFieldDTO> createDataModel(
			CustomFieldDTO dto) {
		return new CustomFieldsSearchDataModel(dto);
	}

	/**
	 * Gets the required values.
	 * 
	 * @return the required values
	 */
	public List<SelectItem> getRequiredValues() {
		List<SelectItem> items = new ArrayList<SelectItem>();
		items.add(new SelectItem(BooleanSelection.BOTH, "Both"));
		items.add(new SelectItem(BooleanSelection.TRUE, "Required"));
		items.add(new SelectItem(BooleanSelection.FALSE, "Optional"));
		return items;
	}

	/**
	 * Gets the custom fields action.
	 * 
	 * @return the custom fields action
	 */
	public CustomFieldsAction getCustomFieldsAction() {
		return customFieldsAction;
	}

	/**
	 * Sets the custom fields action.
	 * 
	 * @param customFieldsAction
	 *            the new custom fields action
	 */
	public void setCustomFieldsAction(CustomFieldsAction customFieldsAction) {
		this.customFieldsAction = customFieldsAction;
	}
}
